home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-8 / args0 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-04  |  3.2 KB  |  146 lines

  1. #include<dos/rdargs.h>
  2. #include<exec/libraries.h>
  3.  
  4. #include<stdio.h>
  5.  
  6. #include<clib/dos_protos.h>
  7. #include<clib/exec_protos.h>
  8.  
  9. #include "gui.h"
  10. #include "idcmp.h"
  11. #include "loadsave.h"
  12. #include "arexx.h"
  13.  
  14. /* The library base global variables */
  15. /* (The different style of opening libraries requires these to be initialised to NULL) */
  16. struct Library* GfxBase = NULL;
  17. struct Library* IntuitionBase = NULL;
  18. struct Library* GadToolsBase = NULL;
  19. struct Library* AslBase = NULL;
  20. struct Library* DosBase = NULL;
  21. struct Library* IFFBase = NULL;
  22. struct Library* RexxSysBase = NULL;
  23.  
  24. /* Need to give prototypes for our functions */
  25. int  createAll(void);
  26. void freeAll(void);
  27. int  openLibs(void);
  28. void closeLibs(void);
  29.  
  30. #define ARGS_TEMPLATE "DEPTH/N,PORTNAME"
  31.  
  32. enum ARGS { ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  33.  
  34. #define DEFAULT_PORTNAME "HELLOPAINTER"
  35. #define DEFAULT_DEPTH    (4)
  36.  
  37. /* The start of the program */
  38. void main()
  39. {
  40.     if(createAll())
  41.         handleIDCMP();
  42.     freeAll();
  43. }
  44.  
  45. static struct RDArgs* rdargs = NULL;
  46.  
  47. int createAll()
  48. {
  49.   LONG args[NUM_ARGS];
  50.   int i;
  51.   /* Initialise our args to NULL */
  52.   /* (This way we will know if an argument was specified) */
  53.   for(i=0; i<NUM_ARGS; i++)
  54.       args[i] = NULL;
  55.   if(openLibs())
  56.     {
  57.       if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  58.         {
  59.             char* portname = (char*)(args[ARG_PORTNAME]);
  60.         LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  61.         UBYTE depth;
  62.             /* Use the default if an argument was not specified */
  63.         if(portname == NULL)
  64.             portname = DEFAULT_PORTNAME;
  65.         if(depthptr == NULL)
  66.             depth = DEFAULT_DEPTH;
  67.         else
  68.             depth = (UBYTE)(*depthptr);
  69.             return createARexxPort(portname) && openGUI(depth,0,0,0);
  70.         }
  71.         else
  72.             printf("Error: could not read arguments\n");
  73.     }
  74.     return FALSE;
  75. }
  76.  
  77. void freeAll()
  78. {
  79.     freeReqs();
  80.     closeGUI();
  81.     freeARexxPort();
  82.     if(rdargs)
  83.         FreeArgs(rdargs);
  84.     closeLibs();
  85. }
  86.  
  87. /* Try to open all the libraries -- return TRUE on success */
  88. int openLibs()
  89. {
  90.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  91.     {
  92.         printf("Error: could not open graphics.library\n");
  93.         return FALSE;
  94.     }
  95.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  96.     {
  97.         printf("Error: could not open intuition.library\n");
  98.         return FALSE;
  99.     }
  100.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  101.     {
  102.         printf("Error: could not open gadtools.library\n");
  103.         return FALSE;
  104.     }
  105.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  106.     {
  107.         printf("Error: could not open asl.library\n");
  108.         return FALSE;
  109.     }
  110.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  111.     {
  112.         printf("Error: could not open dos.library\n");
  113.         return FALSE;
  114.     }
  115.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  116.     {
  117.         printf("Error: could not open iff.library\n");
  118.         return FALSE;
  119.     }
  120.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  121.     {
  122.         printf("Error: could not open rexxsyslib.library\n");
  123.         return FALSE;
  124.     }
  125.   return TRUE;
  126. }
  127.  
  128. /* Close any open library */
  129. void closeLibs()
  130. {
  131.     if(RexxSysBase)
  132.         CloseLibrary(RexxSysBase);
  133.     if(IFFBase)
  134.         CloseLibrary(IFFBase);
  135.     if(DosBase)
  136.         CloseLibrary(DosBase);
  137.     if(AslBase)
  138.         CloseLibrary(AslBase);
  139.     if(GadToolsBase)
  140.         CloseLibrary(GadToolsBase);
  141.     if(IntuitionBase)
  142.         CloseLibrary(IntuitionBase);
  143.     if(GfxBase)
  144.         CloseLibrary(GfxBase);
  145. }
  146.